package com.smartlifev30.mvp.presenter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.smartlifev30.baiwei.Config;
import com.smartlifev30.mvp.module.IGatewayActivityModule;
import com.smartlifev30.mvp.view.IGatewayActivityView;
import com.smartlifev30.network.BaseCallBack;

import smartlifev30.com.sdk.api.CallBack;
import smartlifev30.com.sdk.api.gateway_app.SearchGateway;
import smartlifev30.com.sdk.bean.response.GatewayInfo;
import smartlifev30.com.sdk.bean.response.LoginGatewayResponse;
import smartlifev30.com.sdk.bean.response.LoginResponse;
import smartlifev30.com.sdk.net.baiwei.Client;
import smartlifev30.com.sdk.net.baiwei.NetManager;
import smartlifev30.com.sdk.net.core.IClient;

/**
 * Created by tys on 2018/8/17.
 */

public class GatewayActivityPresent extends BasePresenter<IGatewayActivityView, IGatewayActivityModule> {
    private SearchGateway searchGateway;

    public GatewayActivityPresent(IGatewayActivityView view) {
        super(view);
    }

    public GatewayActivityPresent(IGatewayActivityView view, IGatewayActivityModule model) {
        super(view, model);
    }

    public void searchGateway() {
        if (searchGateway != null) {
            searchGateway.stopSearch();
        }
        searchGateway = new SearchGateway();
        searchGateway.startSearch(new SearchGateway.SearchGatewayCallBack() {
            @Override
            public void addressInfoCallback(GatewayInfo gateways) {
                IGatewayActivityView iGatewayActivityView = view.get();
                if (iGatewayActivityView != null) {
                    iGatewayActivityView.addGateway(gateways);
                }
            }

            @Override
            public void failed(Throwable e, String message) {
                Log.e("0-0-", "搜索网关异常:" + message);
            }
        });
    }

    public void stopSearchGateway() {
        if (searchGateway != null) {
            searchGateway.stopSearch();
        }
    }

    public void entryTargetGateway(final Context context, final GatewayInfo gatewayInfo) {
        IGatewayActivityView iGatewayActivityView = view.get();
        if (iGatewayActivityView != null) {
            iGatewayActivityView.showLoading();
            String ip = gatewayInfo.getIp();
            String port = gatewayInfo.getPort();
            if (!TextUtils.isEmpty(ip) && !TextUtils.isEmpty(port)) {
                //内网
                Client client = new Client.Builder()
                        .writeTimeOut(60)
                        .build();
                NetManager instance = NetManager.getInstance();
                instance.setClient(client);
                instance.setHostAndPort(ip, Integer.parseInt(port));
                loginGateway(context, gatewayInfo, true);
            } else {
                //外网
                Config config = Config.getInstance();
                config.setNetType(0);
                loginGateway(context, gatewayInfo, false);
            }
        }
    }

    private void loginGateway(Context context, GatewayInfo gatewayInfo, boolean isInner) {
        final String userName = "18825260673";  /*model.getUserName(context)*/
        String password = model.getPwd(context);
        IGatewayActivityView iGatewayActivityView = view.get();
        if (!TextUtils.isEmpty(userName) && !TextUtils.isEmpty(password)) {
            model.loginGateway(userName, password, gatewayInfo.getSn(), new BaseCallBack<LoginGatewayResponse>() {
                @Override
                protected void onSuccess(LoginGatewayResponse response) {
                    Config instance = Config.getInstance();
                    if (isInner) {
                        instance.setUserName(userName);
                    }
                    LoginGatewayResponse.UserBean user = response.getUser();
                    String token = user.getToken();
                    instance.setGatewayToken(token);
                    NetManager.getInstance().setUserAndToken(userName, token);
                    iGatewayActivityView.dismissLoading();
                    iGatewayActivityView.toMainActivity();
                }

                @Override
                protected void onFailed(Throwable throwable, int code) {
                    super.onFailed(throwable, code);
                    iGatewayActivityView.dismissLoading();
                }

                @Override
                protected void onTimeOut(String detailMessage) {
                    super.onTimeOut(detailMessage);
                    iGatewayActivityView.dismissLoading();
                }
            });
        }
    }

}
